﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Dusk_Tile_Editor
{
    public partial class Form1 : Form
    {
        SGP.ManagedDirect3D D3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager MTM = SGP.ManagedTextureManager.Instance;

        public TileSet tileSet = new TileSet();

        bool looping = true;

        private Point selectedImageTile = Point.Empty;

        public int imageID;

        private string FolderPath = string.Empty;

        private string BitmapName = string.Empty;

        private string FileName = string.Empty;

        public List<CameraPoint> CameraPoints = new List<CameraPoint>();

        private Rectangle tileSelectionRectangle;

        public List<SpawnPoint> spawnPoints = new List<SpawnPoint>();
        
        public Rectangle TileSelectionRectangle
        {
            get { return tileSelectionRectangle; }
            set { tileSelectionRectangle = value; }
        }

        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public Form1()
        {
            InitializeComponent();
            looping = true;
            tabControl1.TabPages[0].Text = "Tile Set";
            D3D.InitManagedDirect3D(tileSelectionPanel2);
            D3D.InitManagedDirect3D(graphicsPanel1);
            MTM.InitManagedTextureManager(D3D.Device, D3D.Sprite);           
            tileSelectionPanel2.Initialize();
            BitmapName = tileSelectionPanel2.defaultBitmapFileName;
            tileSet = tileSelectionPanel2.tileSet;
            graphicsPanel1.Initialize(tileSet);
            FolderBrowserDialog filePathDlg = new FolderBrowserDialog();
            if (DialogResult.OK == filePathDlg.ShowDialog())
            {
                FolderPath = filePathDlg.SelectedPath;
            }
            comboBox1.SelectedIndex = 4;
            comboBox2.SelectedIndex = 0;
            
        }

        public new void Update()
        {
            tileSet = tileSelectionPanel2.tileSet;
            //graphicsPanel1.tileSet = tileSelectionPanel2.tileSet;
            graphicsPanel1.UpdateTileSet(tileSet);
            graphicsPanel1.imageID = imageID;
            selectedImageTile = tileSelectionPanel2.SelectedTile;
            graphicsPanel1.selectedImageTile = selectedImageTile;
        }

        public void Render()
        {
            graphicsPanel1.Render();
            tileSelectionPanel2.Render();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }

        private void tileSelectionPanel2_MouseClick(object sender, MouseEventArgs e)
        {
            Point offset = new Point(0, 0);
            offset.X += tileSelectionPanel2.AutoScrollPosition.X;
            offset.Y += tileSelectionPanel2.AutoScrollPosition.Y;

            int i = (e.Location.X - offset.X) / tileSet.TileSize.Width;
            int j = (e.Location.Y - offset.Y) / tileSet.TileSize.Height;

            Rectangle rect = new Rectangle(i * tileSelectionPanel2.tileSet.TileSize.Width + offset.X,
                        j * tileSelectionPanel2.tileSet.TileSize.Height + offset.Y,
                        tileSelectionPanel2.tileSet.TileSize.Width, tileSelectionPanel2.tileSet.TileSize.Height);
            if (rect.Contains(e.Location))
            {
                tileSelectionPanel2.SelectedTile = new Point(i, j);
                if (i < tileSet.TileSetSize.Width && j < tileSet.TileSetSize.Height)
                {
                    comboBox1.SelectedIndex = (int)tileSelectionPanel2.Tiles[i, j].TileType;
                    comboBox2.SelectedIndex = (int)tileSelectionPanel2.Tiles[i, j].HasItem;
                }
                else
                {
                    comboBox1.SelectedIndex = comboBox1.MaxDropDownItems + 1;
                    comboBox2.SelectedIndex = (int)Tile.eItem.ITEM_NO_ITEM;
                }
            }
        }

        private void MapWIdthNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            Point[,] tempMap1 = new Point[(int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value];
            Point[,] tempMap2 = new Point[(int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value];
            Point[,] tempMap3 = new Point[(int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value];
            for (int i = 0; i < (int)MapWIdthNumericUpDown.Value; i++)
            {
                for (int j = 0; j < (int)MapHeightNumericUpDown.Value; j++)
                {
                    if (i < graphicsPanel1.mapSize.Width)
                    {
                        tempMap1[i, j] = graphicsPanel1.Layer1[i, j];
                        tempMap2[i, j] = graphicsPanel1.Layer2[i, j];
                        tempMap3[i, j] = graphicsPanel1.Layer3[i, j];
                    }
                    else
                    {
                        tempMap2[i, j] = new Point(tileSet.TileSetSize.Width, tileSet.TileSetSize.Height);
                        tempMap3[i, j] = new Point(tileSet.TileSetSize.Width, tileSet.TileSetSize.Height);
                    }
                }
            }
            graphicsPanel1.Layer1 = tempMap1;
            graphicsPanel1.Layer2 = tempMap2;
            graphicsPanel1.Layer3 = tempMap3;
            graphicsPanel1.SetMapSize((int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value);
        }

        private void MapHeightNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            Point[,] tempMap1 = new Point[(int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value];
            Point[,] tempMap2 = new Point[(int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value];
            Point[,] tempMap3 = new Point[(int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value];
            for (int i = 0; i < (int)MapWIdthNumericUpDown.Value; i++)
            {
                for (int j = 0; j < (int)MapHeightNumericUpDown.Value; j++)
                {
                    if (j < graphicsPanel1.mapSize.Height)
                    {
                        tempMap1[i, j] = graphicsPanel1.Layer1[i, j];
                        tempMap2[i, j] = graphicsPanel1.Layer2[i, j];
                        tempMap3[i, j] = graphicsPanel1.Layer3[i, j];
                    }
                    else
                    {
                        tempMap2[i, j] = new Point(tileSet.TileSetSize.Width, tileSet.TileSetSize.Height);
                        tempMap3[i, j] = new Point(tileSet.TileSetSize.Width, tileSet.TileSetSize.Height);
                    }
                }
            }
            graphicsPanel1.Layer1 = tempMap1;
            graphicsPanel1.Layer2 = tempMap2;
            graphicsPanel1.Layer3 = tempMap3;
            graphicsPanel1.SetMapSize((int)MapWIdthNumericUpDown.Value, (int)MapHeightNumericUpDown.Value);
        }

        private void graphicsPanel1_MouseClick(object sender, MouseEventArgs e)
        {
            Point offset = new Point(0, 0);
            offset.X += graphicsPanel1.AutoScrollPosition.X;
            offset.Y += graphicsPanel1.AutoScrollPosition.Y;
            int i = (e.Location.X - offset.X) / tileSet.TileSize.Width;
            int j = (e.Location.Y - offset.Y) / tileSet.TileSize.Height;

            Rectangle rect = new Rectangle(i * tileSelectionPanel2.tileSet.TileSize.Width + offset.X,
                        j * tileSelectionPanel2.tileSet.TileSize.Height + offset.Y,
                        tileSelectionPanel2.tileSet.TileSize.Width, tileSelectionPanel2.tileSet.TileSize.Height);
            if (e.Button == MouseButtons.Right && rect.Contains(e.Location) && 
                i < graphicsPanel1.mapSize.Width && j < graphicsPanel1.mapSize.Height
                && i > -1 && j > -1)
            {
                
                if (numericUpDown3.Value == 0)
                {
                    tileSelectionPanel2.SelectedTile = graphicsPanel1.Layer1[i, j];
                    graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                    j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                    tileSet.TileSize.Height);
                    if (tileSelectionPanel2.SelectedTile.X < tileSet.TileSetSize.Width && tileSelectionPanel2.SelectedTile.Y < tileSet.TileSetSize.Height)
                        comboBox1.SelectedIndex = (int)tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType;
                    else
                        comboBox1.SelectedIndex = comboBox1.MaxDropDownItems - 1;
                }
                if (numericUpDown3.Value == 1)
                {
                    tileSelectionPanel2.SelectedTile = graphicsPanel1.Layer2[i, j];
                    graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                    j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                    tileSet.TileSize.Height);
                    if (tileSelectionPanel2.SelectedTile.X < tileSet.TileSetSize.Width && tileSelectionPanel2.SelectedTile.Y < tileSet.TileSetSize.Height)
                        comboBox1.SelectedIndex = (int)tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType;
                    else
                        comboBox1.SelectedIndex = comboBox1.MaxDropDownItems - 1;
                }
                if (numericUpDown3.Value == 2)
                {
                    tileSelectionPanel2.SelectedTile = graphicsPanel1.Layer3[i, j] ;
                    graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                    j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                    tileSet.TileSize.Height);
                    if (tileSelectionPanel2.SelectedTile.X < tileSet.TileSetSize.Width && tileSelectionPanel2.SelectedTile.Y < tileSet.TileSetSize.Height)
                    {
                        comboBox1.SelectedIndex = (int)tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType;
                        comboBox2.SelectedIndex = (int)tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].HasItem;
                    }
                    else
                    {
                        comboBox1.SelectedIndex = comboBox1.MaxDropDownItems - 1;
                        comboBox2.SelectedIndex = 0;
                    }
                }
                
            }
            else if (rect.Contains(e.Location) && i < graphicsPanel1.mapSize.Width &&
                     j < graphicsPanel1.mapSize.Height && i > -1 && j > -1)
            {
                if (numericUpDown3.Value == 0)
                {
                    graphicsPanel1.Layer1[i, j] = tileSelectionPanel2.SelectedTile;
                    graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                    j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                    tileSet.TileSize.Height);
                }
                if (numericUpDown3.Value == 1)
                {
                    graphicsPanel1.Layer2[i, j] = tileSelectionPanel2.SelectedTile;
                    graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                    j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                    tileSet.TileSize.Height);
                }
                if (numericUpDown3.Value == 2)
                {
                    graphicsPanel1.Layer3[i, j] = tileSelectionPanel2.SelectedTile;
                    graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                    j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                    tileSet.TileSize.Height);
                }
            }
                    
        }

        bool mouseDown = false;

        private void graphicsPanel1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }

        private void graphicsPanel1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void graphicsPanel1_MouseMove(object sender, MouseEventArgs e)
        {
            if( mouseDown == true )
            {
                Point offset = new Point(0, 0);
                offset.X += graphicsPanel1.AutoScrollPosition.X;
                offset.Y += graphicsPanel1.AutoScrollPosition.Y;

                int i = (e.Location.X - offset.X) / tileSet.TileSize.Width;
                int j = (e.Location.Y - offset.Y) / tileSet.TileSize.Height;
                
                //for (int i = 0; i < graphicsPanel1.mapSize.Width; i++) // <- Derp
                //{
                //    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                //    {
                        Rectangle rect = new Rectangle(i * tileSelectionPanel2.tileSet.TileSize.Width + offset.X,
                                    j * tileSelectionPanel2.tileSet.TileSize.Height + offset.Y,
                                    tileSelectionPanel2.tileSet.TileSize.Width, tileSelectionPanel2.tileSet.TileSize.Height);
                        if (rect.Contains(e.Location) && i < graphicsPanel1.mapSize.Width &&
                            j < graphicsPanel1.mapSize.Height && i > -1 && j > -1)
                        {
                            if (numericUpDown3.Value == 0)
                            {
                                graphicsPanel1.Layer1[i, j] = tileSelectionPanel2.SelectedTile;
                                graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                                j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                                tileSet.TileSize.Height);
                            }
                            if (numericUpDown3.Value == 1)
                            {
                                graphicsPanel1.Layer2[i, j] = tileSelectionPanel2.SelectedTile;
                                graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                                j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                                tileSet.TileSize.Height);
                            }
                            if (numericUpDown3.Value == 2)
                            {
                                graphicsPanel1.Layer3[i, j] = tileSelectionPanel2.SelectedTile;
                                graphicsPanel1.selectedRectangle = new Rectangle(i * tileSet.TileSize.Width + offset.X,
                                j * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                                tileSet.TileSize.Height);
                            }
                        }

               //    }
               //}
            }
        }

        private void TileWidthNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            tileSelectionPanel2.UpdateTileSet((int)TileWidthNumericUpDown.Value, (int)TileHeightNumerocUpDown.Value,
                (int)TileSetWidthNumericUpDown.Value, (int)TileSetHeightNumericUpDown.Value);
        }

        private void TileHeightNumerocUpDown_ValueChanged(object sender, EventArgs e)
        {
            tileSelectionPanel2.UpdateTileSet((int)TileWidthNumericUpDown.Value, (int)TileHeightNumerocUpDown.Value,
                 (int)TileSetWidthNumericUpDown.Value, (int)TileSetHeightNumericUpDown.Value);
        }

        private void TileSetWidthNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            tileSelectionPanel2.UpdateTileSet((int)TileWidthNumericUpDown.Value, (int)TileHeightNumerocUpDown.Value,
                (int)TileSetWidthNumericUpDown.Value, (int)TileSetHeightNumericUpDown.Value);
        }

        private void TileSetHeightNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            tileSelectionPanel2.UpdateTileSet((int)TileWidthNumericUpDown.Value, (int)TileHeightNumerocUpDown.Value, 
                (int)TileSetWidthNumericUpDown.Value, (int)TileSetHeightNumericUpDown.Value);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tileSelectionPanel2.SelectedTile.X < tileSet.TileSetSize.Width && tileSelectionPanel2.SelectedTile.Y < tileSet.TileSetSize.Height)
                switch (comboBox1.SelectedIndex)
                {
                    case (int)Tile.eTileType.TILE_BOTTOMLESS:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_DOOR:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_FIRE:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_GLASS:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_GROUND:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_HEAL:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_ICE:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_TAR:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                    case (int)Tile.eTileType.TILE_WALL:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].TileType = (Tile.eTileType)comboBox1.SelectedIndex;
                        } break;
                }
            else comboBox1.SelectedIndex = comboBox1.MaxDropDownItems + 1;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            graphicsPanel1.Layer = (short)numericUpDown3.Value;
            if (numericUpDown3.Value == 2)
            {
                comboBox2.Enabled = true;
                SpawnGroupBox.Enabled = true;
                groupBox6.Enabled = true;
            }
            else
            {
                comboBox2.Enabled = false;
                SpawnGroupBox.Enabled = false;
                groupBox6.Enabled = false;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (numericUpDown3.Value == 2)
            {
                if(tileSelectionPanel2.SelectedTile.X < tileSet.TileSetSize.Width && tileSelectionPanel2.SelectedTile.Y < tileSet.TileSetSize.Height )
                switch (comboBox2.SelectedIndex)
                {
                    case (int)Tile.eItem.ITEM_NO_ITEM:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].HasItem = (Tile.eItem)comboBox2.SelectedIndex;
                        }break;
                    case (int)Tile.eItem.ITEM_KEY:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].HasItem = (Tile.eItem)comboBox2.SelectedIndex;
                        }break;
                    case (int)Tile.eItem.ITEM_POWER_UP:
                        {
                            tileSelectionPanel2.Tiles[tileSelectionPanel2.SelectedTile.X, tileSelectionPanel2.SelectedTile.Y].HasItem = (Tile.eItem)comboBox2.SelectedIndex;
                        } break;
                }
            }
        }

        private void graphicsPanel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (numericUpDown3.Value == 2)
            {
                Point offset = new Point(0, 0);
                offset.X += graphicsPanel1.AutoScrollPosition.X;
                offset.Y += graphicsPanel1.AutoScrollPosition.Y;

                int i = (e.Location.X - offset.X) / tileSet.TileSize.Width;
                int j = (e.Location.Y - offset.Y) / tileSet.TileSize.Height;

                Rectangle rect = new Rectangle(i * tileSelectionPanel2.tileSet.TileSize.Width + offset.X,
                                    j * tileSelectionPanel2.tileSet.TileSize.Height + offset.Y,
                                    tileSelectionPanel2.tileSet.TileSize.Width, tileSelectionPanel2.tileSet.TileSize.Height);
                if (SpawnPointTypeDropBox.SelectedIndex != -1)
                {
                    SpawnPoint temp = new SpawnPoint();
                    temp.eEnemySpawn = (SpawnPoint.EnemySpawnType)SpawnPointTypeDropBox.SelectedIndex;
                    temp.WorldPos.X = graphicsPanel1.selectedRectangle.X + tileSet.TileSize.Width / 2;
                    temp.WorldPos.Y = graphicsPanel1.selectedRectangle.Y + tileSet.TileSize.Height / 2;
                    spawnPoints.Add(temp);
                    graphicsPanel1.SpawnPoints = spawnPoints;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (numericUpDown3.Value == 2)
            {
                CameraPoint temp = new CameraPoint();
                temp.cameraPoint.X = graphicsPanel1.selectedRectangle.X + tileSet.TileSize.Width / 2;
                temp.cameraPoint.Y = graphicsPanel1.selectedRectangle.Y + tileSet.TileSize.Height / 2;
                CameraPoints.Add(temp);
                graphicsPanel1.CameraPoints = CameraPoints;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (FileName == string.Empty)
            {
                saveAsToolStripMenuItem_Click(sender, e);
                return;
            }

                XElement xRoot = new XElement("FileName");
                

                XAttribute xFile = new XAttribute("File", FileName);
                xRoot.Add(xFile);

                XElement xTilesImage = new XElement("TilesImage");
                xRoot.Add(xTilesImage);

                XAttribute xImageName = new XAttribute("ImageName", BitmapName);
                xTilesImage.Add(xImageName);

                XElement xLevelSize = new XElement("LevelSize");
                xTilesImage.Add(xLevelSize);

                XAttribute xLevelwidth = new XAttribute("Width", graphicsPanel1.mapSize.Width);
                xLevelSize.Add(xLevelwidth);

                XAttribute xLevelHeight = new XAttribute("Height", graphicsPanel1.mapSize.Height);
                xLevelSize.Add(xLevelHeight);

                XElement xTileSize = new XElement("TileSize");
                xLevelSize.Add(xTileSize);

                XAttribute xTileWidth = new XAttribute("Width", tileSet.TileSize.Width);
                xTileSize.Add(xTileWidth);

                XAttribute xTileHeight = new XAttribute("Height", tileSet.TileSize.Height);
                xTileSize.Add(xTileHeight);

                XElement xGroundLayer = new XElement("GroundLayer");
                xTileSize.Add(xGroundLayer);

                for (int i = 0; i < graphicsPanel1.mapSize.Width; i++)
                {
                    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                    {

                        selectedImageTile = graphicsPanel1.Layer1[i, j];

                        if (selectedImageTile.X < tileSelectionPanel2.tileSet.TileSetSize.Width && selectedImageTile.Y < tileSelectionPanel2.tileSet.TileSetSize.Height)
                        {
                            XElement xTile = new XElement("Tile");
                            xGroundLayer.Add(xTile);

                            switch (tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].TileType)
                            {
                                case Tile.eTileType.TILE_BOTTOMLESS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Bottomless");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_DOOR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Door");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_FIRE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Fire");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GLASS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Glass");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GROUND:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ground");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_HEAL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Heal");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_ICE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ice");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_TAR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Tar");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_WALL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Wall");
                                        xTile.Add(xName);
                                    } break;
                            }
                            XAttribute xImagePosX = new XAttribute("ImagePosX", selectedImageTile.X);
                            xTile.Add(xImagePosX);

                            XAttribute xImagePosY = new XAttribute("ImagePosY", selectedImageTile.Y);
                            xTile.Add(xImagePosY);

                            XAttribute xWorldX = new XAttribute("WorldX", i * tileSet.TileSize.Width);
                            xTile.Add(xWorldX);

                            XAttribute xWorldY = new XAttribute("WorldY", j * tileSet.TileSize.Height);
                            xTile.Add(xWorldY);
                        }
                    }


                }

                XElement xEdgingLayer = new XElement("EdgingLayer");
                xTileSize.Add(xEdgingLayer);

                for (int i = 0; i < graphicsPanel1.mapSize.Width; i++)
                {
                    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                    {

                        selectedImageTile = graphicsPanel1.Layer2[i, j];

                        if (selectedImageTile.X < tileSet.TileSetSize.Width && selectedImageTile.Y < tileSet.TileSetSize.Height)
                        {
                            XElement xTile = new XElement("Tile");
                            xEdgingLayer.Add(xTile);

                            switch (tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].TileType)
                            {
                                case Tile.eTileType.TILE_BOTTOMLESS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Bottomless");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_DOOR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Door");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_FIRE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Fire");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GLASS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Glass");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GROUND:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ground");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_HEAL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Heal");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_ICE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ice");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_TAR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Tar");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_WALL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Wall");
                                        xTile.Add(xName);
                                    } break;
                            }
                            XAttribute xImagePosX = new XAttribute("ImagePosX", selectedImageTile.X);
                            xTile.Add(xImagePosX);

                            XAttribute xImagePosY = new XAttribute("ImagePosY", selectedImageTile.Y);
                            xTile.Add(xImagePosY);

                            XAttribute xWorldX = new XAttribute("WorldX", i * tileSet.TileSize.Width);
                            xTile.Add(xWorldX);

                            XAttribute xWorldY = new XAttribute("WorldY", j * tileSet.TileSize.Height);
                            xTile.Add(xWorldY);
                        }
                    }


                }

                XElement xEventLayer = new XElement("EventLayer");
                xTileSize.Add(xEventLayer);

                for (int i = 0; i < graphicsPanel1.mapSize.Width; i++)
                {
                    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                    {

                        selectedImageTile = graphicsPanel1.Layer3[i, j];

                        if (selectedImageTile.X < tileSet.TileSetSize.Width && selectedImageTile.Y < tileSet.TileSetSize.Height)
                        {
                            XElement xTile = new XElement("Tile");
                            xEventLayer.Add(xTile);

                            switch (tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].TileType)
                            {
                                case Tile.eTileType.TILE_BOTTOMLESS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Bottomless");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_DOOR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Door");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_FIRE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Fire");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GLASS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Glass");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GROUND:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ground");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_HEAL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Heal");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_ICE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ice");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_TAR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Tar");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_WALL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Wall");
                                        xTile.Add(xName);
                                    } break;
                            }
                            XAttribute xImagePosX = new XAttribute("ImagePosX", selectedImageTile.X);
                            xTile.Add(xImagePosX);

                            XAttribute xImagePosY = new XAttribute("ImagePosY", selectedImageTile.Y);
                            xTile.Add(xImagePosY);

                            XAttribute xWorldX = new XAttribute("WorldX", i * tileSet.TileSize.Width);
                            xTile.Add(xWorldX);

                            XAttribute xWorldY = new XAttribute("WorldY", j * tileSet.TileSize.Height);
                            xTile.Add(xWorldY);

                            XAttribute xHasItem = new XAttribute("HasItem", (int)tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].HasItem);
                            xTile.Add(xHasItem);
                        }
                    }
                }

                XElement xCameraPoints = new XElement("CameraPoints");
                xTileSize.Add(xCameraPoints);
                for (int i = 0; i < CameraPoints.Count; i++)
                {
                    XElement xCameraPoint = new XElement("CameraPoint");
                    xCameraPoints.Add(xCameraPoint);
                    XAttribute xWorldX = new XAttribute("WorldX", CameraPoints[i].cameraPoint.X);
                    xCameraPoint.Add(xWorldX);
                    XAttribute xWorldY = new XAttribute("WorldY", CameraPoints[i].cameraPoint.Y);
                    xCameraPoint.Add(xWorldY);
                }

                XElement xSpawnPoints = new XElement("SpawnPoints");
                xTileSize.Add(xSpawnPoints);

                for (int i = 0; i < spawnPoints.Count; i++)
                {

                    XElement xSpawnPoint = new XElement("SpawnPoint");
                    xSpawnPoints.Add(xSpawnPoint);
                    XAttribute xWorldX = new XAttribute("WorldX", spawnPoints[i].WorldPos.X);
                    xSpawnPoint.Add(xWorldX);
                    XAttribute xWorldY = new XAttribute("WorldY", spawnPoints[i].WorldPos.Y);
                    xSpawnPoint.Add(xWorldY);
                    XAttribute xSpawnType = new XAttribute("Type", (int)spawnPoints[i].eEnemySpawn);
                    xSpawnPoint.Add(xSpawnType);
                }
                xRoot.Save(FolderPath + '\\' + FileName);
            }
            
        


        private void button2_Click(object sender, EventArgs e)
        {
            spawnPoints.Clear();
            graphicsPanel1.SpawnPoints.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CameraPoints.Clear();
            graphicsPanel1.CameraPoints.Clear();
        }

        private void openMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog opDlg = new OpenFileDialog();
            opDlg.Filter = "(*.xml )|*.xml";
            if (DialogResult.OK == opDlg.ShowDialog())
            {
                FileName = opDlg.SafeFileName;

                XElement xRoot = XElement.Load(opDlg.FileName);

                XElement xTilesImage = xRoot.Element("TilesImage");

                XAttribute xImageName = xTilesImage.Attribute("ImageName");
                BitmapName = xImageName.Value;

                if( Directory.Exists( FolderPath + '\\' + BitmapName ))
                {
                    tileSelectionPanel2.tileSet.TileSetBitMap = new Bitmap(FolderPath + '\\' + BitmapName);   
                    MTM.ReleaseTexture(tileSelectionPanel2.ImageID);
                    MTM.LoadTexture(FolderPath + '\\' + BitmapName, 0);
                }
                else
                {
                    OpenFileDialog someDlg = new OpenFileDialog();
                    someDlg.Filter = "(*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png";
                    if (DialogResult.OK == someDlg.ShowDialog())
                    {
                        BitmapName = someDlg.SafeFileName;
                        tileSelectionPanel2.tileSet.TileSetBitMap = new Bitmap(someDlg.FileName);
                        MTM.ReleaseTexture(tileSelectionPanel2.ImageID);
                        tileSelectionPanel2.ImageID = MTM.LoadTexture(someDlg.FileName, 0);
                    }
                }
                

                XElement xLevelSize = xTilesImage.Element("LevelSize");

                XAttribute xLevelWidth = xLevelSize.Attribute("Width");
                int levelWidth = int.Parse(xLevelWidth.Value);

                XAttribute xLevelHeight = xLevelSize.Attribute("Height");
                int levelHeight = int.Parse(xLevelHeight.Value);

                MapHeightNumericUpDown.Value = levelHeight;
                MapWIdthNumericUpDown.Value = levelWidth;

                graphicsPanel1.Layer1 = new Point[levelWidth, levelHeight];
                graphicsPanel1.Layer2 = new Point[levelWidth, levelHeight];
                graphicsPanel1.Layer3 = new Point[levelWidth, levelHeight];

                for (int i = 0; i < levelWidth; i++)
                {
                    for (int j = 0; j < levelHeight; j++)
                    {
                        graphicsPanel1.Layer1[i, j] = new Point(levelHeight, levelWidth);
                        graphicsPanel1.Layer2[i, j] = new Point(levelHeight, levelWidth);
                        graphicsPanel1.Layer3[i, j] = new Point(levelHeight, levelWidth);
                    }
                }

                XElement xTileSize = xLevelSize.Element("TileSize");

                XAttribute xTileWidth = xTileSize.Attribute("Width");
                int tileWidth = int.Parse(xTileWidth.Value);

                XAttribute xTileHeight = xTileSize.Attribute("Height");
                int tileHeight = int.Parse(xTileHeight.Value);

                XElement xGroundLayer = xTileSize.Element("GroundLayer");

                IEnumerable<XElement> xGroundTiles = xGroundLayer.Elements();
                foreach (XElement xTile in xGroundTiles)
                {
                    XAttribute xName = xTile.Attribute("Name");
                    string TileType = xName.Value;

                    XAttribute xImagePosX = xTile.Attribute("ImagePosX");
                    int imagePosX = int.Parse(xImagePosX.Value);

                    XAttribute xImagePosY = xTile.Attribute("ImagePosY");
                    int imagePosY = int.Parse(xImagePosY.Value);

                    XAttribute xWorldX = xTile.Attribute("WorldX");
                    int WorldX = int.Parse(xWorldX.Value);

                    XAttribute xWorldY = xTile.Attribute("WorldY");
                    int WorldY = int.Parse(xWorldY.Value);
                    if (imagePosX >= tileSelectionPanel2.tileSet.TileSetSize.Width)
                    {
                        tileSelectionPanel2.UpdateTileSet(tileWidth, tileHeight, imagePosX + 1, tileSelectionPanel2.tileSet.TileSetSize.Height);
                        TileSetWidthNumericUpDown.Value = tileSelectionPanel2.tileSet.TileSetSize.Width;
                    }

                    if (imagePosY >= tileSelectionPanel2.tileSet.TileSetSize.Height)
                    {
                        tileSelectionPanel2.UpdateTileSet(tileWidth, tileHeight, tileSelectionPanel2.tileSet.TileSetSize.Width, imagePosY + 1);
                        TileSetWidthNumericUpDown.Value = tileSelectionPanel2.tileSet.TileSetSize.Height;
                    }
                    

                    if (TileType == "Ground")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_GROUND;
                    }
                    else if (TileType == "Fire")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_FIRE;
                    }
                    else if (TileType == "Ice")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_ICE;
                    }
                    else if (TileType == "Tar")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_TAR;
                    }
                    else if (TileType == "Heal")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_HEAL;
                    }
                    else if (TileType == "Glass")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_GLASS;
                    }
                    else if (TileType == "Bottomless")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_BOTTOMLESS;
                    }
                    else if (TileType == "Wall")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_WALL;
                    }
                    else if (TileType == "Door")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_DOOR;
                    }

                    graphicsPanel1.Layer1[WorldX / (tileWidth), WorldY / (tileHeight)] = new Point(imagePosX, imagePosY);
                }

                XElement xEdgingLayer = xTileSize.Element("EdgingLayer");
                IEnumerable<XElement> xEdgingTiles = xEdgingLayer.Elements();
                foreach (XElement xTile in xEdgingTiles)
                {
                    XAttribute xName = xTile.Attribute("Name");
                    string TileType = xName.Value;

                    XAttribute xImagePosX = xTile.Attribute("ImagePosX");
                    int imagePosX = int.Parse(xImagePosX.Value);

                    XAttribute xImagePosY = xTile.Attribute("ImagePosY");
                    int imagePosY = int.Parse(xImagePosY.Value);

                    XAttribute xWorldX = xTile.Attribute("WorldX");
                    int WorldX = int.Parse(xWorldX.Value);

                    XAttribute xWorldY = xTile.Attribute("WorldY");
                    int WorldY = int.Parse(xWorldY.Value);
                    if (imagePosX >= tileSelectionPanel2.tileSet.TileSetSize.Width)
                    {
                        tileSelectionPanel2.UpdateTileSet(tileWidth, tileHeight, imagePosX + 1, tileSelectionPanel2.tileSet.TileSetSize.Height);
                        TileSetWidthNumericUpDown.Value = tileSelectionPanel2.tileSet.TileSetSize.Width;
                    }

                    if (imagePosY >= tileSelectionPanel2.tileSet.TileSetSize.Height)
                    {
                        tileSelectionPanel2.UpdateTileSet(tileWidth, tileHeight, tileSelectionPanel2.tileSet.TileSetSize.Width, imagePosY + 1);
                        TileSetWidthNumericUpDown.Value = tileSelectionPanel2.tileSet.TileSetSize.Height;
                    }


                    if (TileType == "Ground")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_GROUND;
                    }
                    else if (TileType == "Fire")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_FIRE;
                    }
                    else if (TileType == "Ice")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_ICE;
                    }
                    else if (TileType == "Tar")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_TAR;
                    }
                    else if (TileType == "Heal")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_HEAL;
                    }
                    else if (TileType == "Glass")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_GLASS;
                    }
                    else if (TileType == "Bottomless")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_BOTTOMLESS;
                    }
                    else if (TileType == "Wall")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_WALL;
                    }
                    else if (TileType == "Door")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_DOOR;
                    }

                    graphicsPanel1.Layer2[WorldX / (tileWidth), WorldY / (tileHeight)] = new Point(imagePosX, imagePosY);
                }

                XElement xEventLayer = xTileSize.Element("EventLayer");

                IEnumerable<XElement> xEventTiles = xEventLayer.Elements();
                foreach (XElement xTile in xEventTiles)
                {
                    XAttribute xName = xTile.Attribute("Name");
                    string TileType = xName.Value;

                    XAttribute xImagePosX = xTile.Attribute("ImagePosX");
                    int imagePosX = int.Parse(xImagePosX.Value);

                    XAttribute xImagePosY = xTile.Attribute("ImagePosY");
                    int imagePosY = int.Parse(xImagePosY.Value);

                    XAttribute xWorldX = xTile.Attribute("WorldX");
                    int WorldX = int.Parse(xWorldX.Value);

                    XAttribute xWorldY = xTile.Attribute("WorldY");
                    int WorldY = int.Parse(xWorldY.Value);
                    if (imagePosX >= tileSelectionPanel2.tileSet.TileSetSize.Width)
                    {
                        tileSelectionPanel2.UpdateTileSet(tileWidth, tileHeight, imagePosX + 1, tileSelectionPanel2.tileSet.TileSetSize.Height);
                        TileSetWidthNumericUpDown.Value = tileSelectionPanel2.tileSet.TileSetSize.Width;
                    }

                    if (imagePosY >= tileSelectionPanel2.tileSet.TileSetSize.Height)
                    {
                        tileSelectionPanel2.UpdateTileSet(tileWidth, tileHeight, tileSelectionPanel2.tileSet.TileSetSize.Width, imagePosY + 1);
                        TileSetWidthNumericUpDown.Value = tileSelectionPanel2.tileSet.TileSetSize.Height;
                    }

                    XAttribute xHasItem = xTile.Attribute("HasItem");
                    int Item = int.Parse(xHasItem.Value);

                    switch (Item)
                    {
                        case (int)Tile.eItem.ITEM_NO_ITEM:
                            {
                                tileSelectionPanel2.Tiles[imagePosX, imagePosY].HasItem = Tile.eItem.ITEM_NO_ITEM;
                            } break;
                        case (int)Tile.eItem.ITEM_KEY:
                            {
                                tileSelectionPanel2.Tiles[imagePosX, imagePosY].HasItem = Tile.eItem.ITEM_KEY;
                            } break;
                        case (int)Tile.eItem.ITEM_POWER_UP:
                            {
                                tileSelectionPanel2.Tiles[imagePosX, imagePosY].HasItem = Tile.eItem.ITEM_POWER_UP;
                            } break;
                    }


                    if (TileType == "Ground")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_GROUND;
                    }
                    else if (TileType == "Fire")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_FIRE;
                    }
                    else if (TileType == "Ice")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_ICE;
                    }
                    else if (TileType == "Tar")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_TAR;
                    }
                    else if (TileType == "Heal")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_HEAL;
                    }
                    else if (TileType == "Glass")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_GLASS;
                    }
                    else if (TileType == "Bottomless")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_BOTTOMLESS;
                    }
                    else if (TileType == "Wall")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_WALL;
                    }
                    else if (TileType == "Door")
                    {
                        tileSelectionPanel2.Tiles[imagePosX, imagePosY].TileType = Tile.eTileType.TILE_DOOR;
                    }

                    graphicsPanel1.Layer3[WorldX / (tileWidth), WorldY / (tileHeight)] = new Point(imagePosX, imagePosY);
                }

                XElement xCameraPoints = xTileSize.Element("CameraPoints");

                IEnumerable<XElement> xCamPoints = xCameraPoints.Elements();
                CameraPoints.Clear();
                foreach (XElement xCameraPoint in xCamPoints)
                {
                    XAttribute xWorldX = xCameraPoint.Attribute("WorldX" );
                    int WorldX = int.Parse(xWorldX.Value);

                    XAttribute xWorldY = xCameraPoint.Attribute("WorldY" );
                    int WorldY = int.Parse(xWorldY.Value);
                    CameraPoint temp = new CameraPoint();
                    temp.cameraPoint.X = WorldX;
                    temp.cameraPoint.Y = WorldY;
                    CameraPoints.Add(temp);
                }
                graphicsPanel1.CameraPoints = CameraPoints;

                XElement xSpawnPoints = xTileSize.Element("SpawnPoints");

                IEnumerable<XElement> xSpawningPoints = xSpawnPoints.Elements();
                spawnPoints.Clear();
                foreach (XElement xSpawnPoint in xSpawningPoints)
                {
                    XAttribute xWorldX = xSpawnPoint.Attribute("WorldX");
                    int WorldX = int.Parse(xWorldX.Value);

                    XAttribute xWorldY = xSpawnPoint.Attribute("WorldY");
                    int WorldY = int.Parse(xWorldY.Value);

                    XAttribute xType = xSpawnPoint.Attribute("Type");
                    int spawnType = int.Parse(xType.Value);

                    SpawnPoint temp = new SpawnPoint();
                    temp.WorldPos.X = WorldX;
                    temp.WorldPos.Y = WorldY;

                    switch (spawnType)
                    {
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_BOSS_BOMBRO:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_BOSS_BOMBRO;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_BOSS_RIOTOFFICER:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_BOSS_RIOTOFFICER;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_BOSS_ROCKGOLEM:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_BOSS_ROCKGOLEM;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_BOSS_SPEEDREAVER:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_BOSS_SPEEDREAVER;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_NINJA:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_NINJA;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_PEON:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_PEON;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_PLAYER:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_PLAYER;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_SHIELDED:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_SHIELDED;
                            } break;
                        case (int)SpawnPoint.EnemySpawnType.SPAWN_SNIPER:
                            {
                                temp.eEnemySpawn = SpawnPoint.EnemySpawnType.SPAWN_SNIPER;
                            } break;
                    }

                    
                    spawnPoints.Add(temp);
                    
                }
                graphicsPanel1.SpawnPoints = spawnPoints;
            }

            
        }

        private void openTileSetToolStripMenuItem_Click(object sender, EventArgs e)
        {

            OpenFileDialog opDlg = new OpenFileDialog();
            opDlg.Filter = "(*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png";
            if (DialogResult.OK == opDlg.ShowDialog())
            {
                BitmapName = opDlg.SafeFileName;
                tileSelectionPanel2.tileSet.TileSetBitMap = new Bitmap(opDlg.FileName);
                MTM.ReleaseTexture(tileSelectionPanel2.ImageID);
                tileSelectionPanel2.ImageID = MTM.LoadTexture(opDlg.FileName, 0);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.*";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "xml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement xRoot = new XElement("FileName");

                
                string temp = dlg.FileName;
                string file = string.Empty;
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i] == '/' || temp[i] == '\\')
                    {
                        file = string.Empty;

                    }
                    else
                    {
                        file += temp[i];
                    }
                }
                FileName = file;
                XAttribute xFile = new XAttribute("File", file);
                xRoot.Add(xFile);


                XElement xTilesImage = new XElement("TilesImage");
                xRoot.Add(xTilesImage);

                XAttribute xImageName = new XAttribute("ImageName", BitmapName);
                xTilesImage.Add(xImageName);

                XElement xLevelSize = new XElement("LevelSize");
                xTilesImage.Add(xLevelSize);

                XAttribute xLevelwidth = new XAttribute("Width", graphicsPanel1.mapSize.Width);
                xLevelSize.Add(xLevelwidth);

                XAttribute xLevelHeight = new XAttribute("Height", graphicsPanel1.mapSize.Height);
                xLevelSize.Add(xLevelHeight);

                XElement xTileSize = new XElement("TileSize");
                xLevelSize.Add(xTileSize);

                XAttribute xTileWidth = new XAttribute("Width", tileSet.TileSize.Width);
                xTileSize.Add(xTileWidth);

                XAttribute xTileHeight = new XAttribute("Height", tileSet.TileSize.Height);
                xTileSize.Add(xTileHeight);

                XElement xGroundLayer = new XElement("GroundLayer");
                xTileSize.Add(xGroundLayer);

                for (int i = 0; i < MapHeightNumericUpDown.Value; i++)
                {
                    for (int j = 0; j < MapHeightNumericUpDown.Value; j++)
                    {

                        selectedImageTile = graphicsPanel1.Layer1[i, j];

                        if (selectedImageTile.X < tileSet.TileSetSize.Width && selectedImageTile.Y < tileSet.TileSetSize.Height)
                        {
                            XElement xTile = new XElement("Tile");
                            xGroundLayer.Add(xTile);

                            switch (tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].TileType)
                            {
                                case Tile.eTileType.TILE_BOTTOMLESS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Bottomless");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_DOOR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Door");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_FIRE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Fire");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GLASS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Glass");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GROUND:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ground");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_HEAL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Heal");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_ICE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ice");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_TAR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Tar");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_WALL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Wall");
                                        xTile.Add(xName);
                                    } break;
                            }
                            XAttribute xImagePosX = new XAttribute("ImagePosX", selectedImageTile.X);
                            xTile.Add(xImagePosX);

                            XAttribute xImagePosY = new XAttribute("ImagePosY", selectedImageTile.Y);
                            xTile.Add(xImagePosY);

                            XAttribute xWorldX = new XAttribute("WorldX", i * tileSet.TileSize.Width);
                            xTile.Add(xWorldX);

                            XAttribute xWorldY = new XAttribute("WorldY", j * tileSet.TileSize.Height);
                            xTile.Add(xWorldY);
                        }
                    }


                }

                XElement xEdgingLayer = new XElement("EdgingLayer");
                xTileSize.Add(xEdgingLayer);

                for (int i = 0; i < MapHeightNumericUpDown.Value; i++)
                {
                    for (int j = 0; j < MapHeightNumericUpDown.Value; j++)
                    {

                        selectedImageTile = graphicsPanel1.Layer2[i, j];

                        if (selectedImageTile.X < tileSet.TileSetSize.Width && selectedImageTile.Y < tileSet.TileSetSize.Height)
                        {
                            XElement xTile = new XElement("Tile");
                            xEdgingLayer.Add(xTile);

                            switch (tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].TileType)
                            {
                                case Tile.eTileType.TILE_BOTTOMLESS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Bottomless");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_DOOR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Door");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_FIRE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Fire");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GLASS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Glass");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GROUND:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ground");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_HEAL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Heal");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_ICE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ice");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_TAR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Tar");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_WALL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Wall");
                                        xTile.Add(xName);
                                    } break;
                            }
                            XAttribute xImagePosX = new XAttribute("ImagePosX", selectedImageTile.X);
                            xTile.Add(xImagePosX);

                            XAttribute xImagePosY = new XAttribute("ImagePosY", selectedImageTile.Y);
                            xTile.Add(xImagePosY);

                            XAttribute xWorldX = new XAttribute("WorldX", i * tileSet.TileSize.Width);
                            xTile.Add(xWorldX);

                            XAttribute xWorldY = new XAttribute("WorldY", j * tileSet.TileSize.Height);
                            xTile.Add(xWorldY);
                        }
                    }


                }

                XElement xEventLayer = new XElement("EventLayer");
                xTileSize.Add(xEventLayer);

                for (int i = 0; i < MapHeightNumericUpDown.Value; i++)
                {
                    for (int j = 0; j < MapHeightNumericUpDown.Value; j++)
                    {

                        selectedImageTile = graphicsPanel1.Layer3[i, j];

                        if (selectedImageTile.X < tileSet.TileSetSize.Width && selectedImageTile.Y < tileSet.TileSetSize.Height)
                        {
                            XElement xTile = new XElement("Tile");
                            xEventLayer.Add(xTile);

                            switch (tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].TileType)
                            {
                                case Tile.eTileType.TILE_BOTTOMLESS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Bottomless");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_DOOR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Door");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_FIRE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Fire");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GLASS:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Glass");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_GROUND:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ground");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_HEAL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Heal");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_ICE:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Ice");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_TAR:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Tar");
                                        xTile.Add(xName);
                                    } break;
                                case Tile.eTileType.TILE_WALL:
                                    {
                                        XAttribute xName = new XAttribute("Name", "Wall");
                                        xTile.Add(xName);
                                    } break;
                            }
                            XAttribute xImagePosX = new XAttribute("ImagePosX", selectedImageTile.X);
                            xTile.Add(xImagePosX);

                            XAttribute xImagePosY = new XAttribute("ImagePosY", selectedImageTile.Y);
                            xTile.Add(xImagePosY);

                            XAttribute xWorldX = new XAttribute("WorldX", i * tileSet.TileSize.Width);
                            xTile.Add(xWorldX);

                            XAttribute xWorldY = new XAttribute("WorldY", j * tileSet.TileSize.Height);
                            xTile.Add(xWorldY);

                            XAttribute xHasItem = new XAttribute("HasItem", (int)tileSelectionPanel2.Tiles[selectedImageTile.X, selectedImageTile.Y].HasItem);
                            xTile.Add(xHasItem);
                        }
                    }
                }

                XElement xCameraPoints = new XElement("CameraPoints");
                xTileSize.Add(xCameraPoints);
                for (int i = 0; i < CameraPoints.Count; i++)
                {
                    XElement xCameraPoint = new XElement("CameraPoint");
                    xCameraPoints.Add(xCameraPoint);
                    XAttribute xWorldX = new XAttribute("WorldX", CameraPoints[i].cameraPoint.X);
                    xCameraPoint.Add(xWorldX);
                    XAttribute xWorldY = new XAttribute("WorldY", CameraPoints[i].cameraPoint.Y);
                    xCameraPoint.Add(xWorldY);
                }

                XElement xSpawnPoints = new XElement("SpawnPoints");
                xTileSize.Add(xSpawnPoints);

                for (int i = 0; i < spawnPoints.Count; i++)
                {

                    XElement xSpawnPoint = new XElement("SpawnPoint");
                    xSpawnPoints.Add(xSpawnPoint);
                    XAttribute xWorldX = new XAttribute("WorldX", spawnPoints[i].WorldPos.X);
                    xSpawnPoint.Add(xWorldX);
                    XAttribute xWorldY = new XAttribute("WorldY", spawnPoints[i].WorldPos.Y);
                    xSpawnPoint.Add(xWorldY);
                    XAttribute xSpawnType = new XAttribute("Type", (int)spawnPoints[i].eEnemySpawn);
                    xSpawnPoint.Add(xSpawnType);
                }
                xRoot.Save(dlg.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            looping = false;
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (numericUpDown3.Value == 2)
            {
                SpawnPoint temp = new SpawnPoint();
                temp.eEnemySpawn = (SpawnPoint.EnemySpawnType)SpawnPointTypeDropBox.SelectedIndex;
                temp.WorldPos.X = graphicsPanel1.selectedRectangle.X + tileSet.TileSize.Width / 2;
                temp.WorldPos.Y = graphicsPanel1.selectedRectangle.Y + tileSet.TileSize.Height / 2;
                for (int i = 0; i < spawnPoints.Count; i++)
                {
                    if (temp.WorldPos == spawnPoints[i].WorldPos)
                    {
                        spawnPoints.RemoveAt(i);
                    }
                }
                graphicsPanel1.SpawnPoints = spawnPoints;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (numericUpDown3.Value == 2)
            {
                CameraPoint temp = new CameraPoint();
                temp.cameraPoint.X = graphicsPanel1.selectedRectangle.X + tileSet.TileSize.Width / 2;
                temp.cameraPoint.Y = graphicsPanel1.selectedRectangle.Y + tileSet.TileSize.Height / 2;
                for (int i = 0; i < CameraPoints.Count; i++)
                {
                    if (temp.cameraPoint == CameraPoints[i].cameraPoint)
                    {
                        CameraPoints.RemoveAt(i);
                    }
                }
                graphicsPanel1.CameraPoints = CameraPoints;
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphicsPanel1.mapSize.Width = 1;
            graphicsPanel1.mapSize.Height = 1;
            graphicsPanel1.Layer1 = new Point[1, 1];
            graphicsPanel1.Layer2 = new Point[1, 1];
            graphicsPanel1.Layer3 = new Point[1, 1];
            spawnPoints.Clear();
            CameraPoints.Clear();
            graphicsPanel1.CameraPoints = CameraPoints;
            graphicsPanel1.SpawnPoints = spawnPoints;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if( numericUpDown3.Value == 0 )
            {
                for (int i = 0; i < graphicsPanel1.mapSize.Width; i++)
                {
                    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                    {
                        
                            graphicsPanel1.Layer1[ i, j ] =  selectedImageTile;     
                    }
                }
            }
            if( numericUpDown3.Value == 1 )
            {
                for (int i = 0; i < graphicsPanel1.mapSize.Width; i++)
                {
                    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                    {
                        
                        graphicsPanel1.Layer2[ i, j ] =  selectedImageTile;
                    }
                }
            }
            if( numericUpDown3.Value == 2 )
            {
                for (int i = 0; i < graphicsPanel1.mapSize.Width; i++)
                {
                    for (int j = 0; j < graphicsPanel1.mapSize.Height; j++)
                    {

                        graphicsPanel1.Layer3[i, j] = selectedImageTile;
                    }
                }
            }
        }
    }
}
